package com.lyf.shop.dao.Impl;

import com.lyf.shop.dao.OrderDao;
import com.lyf.shop.dao.ProductDao;
import com.lyf.shop.entity.Page;
import com.lyf.shop.entity.Product;
import com.lyf.shop.entity.Type;
import com.lyf.shop.util.DataBaseUtil;
import com.lyf.shop.util.StrUtils;
import com.sun.org.apache.bcel.internal.generic.NEWARRAY;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.lang.reflect.InvocationTargetException;
import java.security.KeyStore;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ProductDaoImpl implements ProductDao {
    private QueryRunner qr = new QueryRunner();
    @Override
    public Long getCountByTid(int tid) throws SQLException {
        return qr.query(DataBaseUtil.getConnection(),
                "select count(pid) from product where tid=?",new ScalarHandler<>(),tid);
    }

    @Override
    public List<Product> getProListByTid(int tid, Page page) throws SQLException {
        return qr.query(DataBaseUtil.getConnection(),
                "select * from product where tid=? limit ?,?",new BeanListHandler<>(Product.class),
                tid,page.getStartRows(),page.getPageSize());
    }

    @Override
    public Product getProById(int pid) throws SQLException {
        return qr.query(DataBaseUtil.getConnection(),
                "select * from product where pid=?",new BeanHandler<>(Product.class),pid);
    }

    @Override
    public List<Product> getProListByKey(String pname, String ptime) throws SQLException {
        String sql = "select p.pid,p.tid,p.pname,p.ptime,p.pimage,p.pprice,p.pstate,p.pinfo,t.tid,t.tname,t.tinfo from product p,type t where t.tid=p.tid";
        List<Object> param = new ArrayList<>();
        if (StrUtils.empty(pname)) {
            sql+=" and pname like ?";
            param.add("%"+pname+"%");
        }
        if (StrUtils.empty(ptime)){
            sql+=" and ptime=?";
            param.add(ptime);
        }
        List<Map<String, Object>> query = qr.query(DataBaseUtil.getConnection(), sql, new MapListHandler(), param.toArray());
        if(query==null){
            return null;
        }
        List<Product> proList = new ArrayList<>();
        for (Map<String, Object> map : query) {
            Product p  = new Product();
            Type t = new Type();
            try {
                BeanUtils.populate(p,map);
                BeanUtils.populate(t,map);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            p.setType(t);
            proList.add(p);
        }
        return proList;
    }



    @Override
    public int addPro(Product product) throws SQLException {
        return qr.update(DataBaseUtil.getConnection(),"insert into product values (default ,?,?,?,?,?,?,?) ",
                product.getTid(),product.getPname(),product.getPtime(),product.getPimage(),product.getPprice(), product.getPstate(),product.getPinfo());

    }
}
